<template>
  <div>
    <el-drawer
      class="drawerWithButton"
      title="按钮设置"
      lock-scroll
      :direction="direction"
      :modal-append-to-body="false"
      :size="size"
      append-to-body
      :visible.sync="drawer"
      :wrapper-closable="false"
    >
      <div class="mainbox">
        <div class="table-container table-fullscreen">
          <div class="table-opt-container">
            <div class="flex-1" />
            <!--   v-permission="['APPLICATIONSYSTEM_ADDBUTTON_BUTTON']" -->
            <el-button
              class="filter-item"
              type="primary"
              @click="handleEdit('create', '')"
            >
              新增按钮
            </el-button>
          </div>
          <el-table
            class="buttonTable"
            ref="table"
            v-loading="loading"
            border
            :data="buttonList"
            highlight-current-row
            style="width: 100%;"
            row-key="buttonId"
            :header-cell-style="{
              backgroundColor: '#f2f2f2'
            }"
          >
            <template slot="empty">
              <p>{{ $store.getters.dataText }}</p>
            </template>
            <el-table-column
              key="label"
              prop="label"
              label="按钮名称"
            />
            <el-table-column
              key="buttonType"
              prop="buttonType"
              label="类型"
            >
              <template slot-scope="scope">
                {{ scope.row.buttonType==0?'内置':'自定义' }}
              </template>
            </el-table-column>
            <el-table-column
              key="status"
              prop="status"
              label="显示"
              show-overflow-tooltip
            >
              <template slot="header">
                <el-checkbox @change="handleSelectionChange" :indeterminate="isIndeterminate" v-model="checkAll">
                  显示
                </el-checkbox>
              </template>
              <template slot-scope="scope">
                <el-checkbox v-model="scope.row.status" :true-label="1" :false-label="0"
                             @change="changeStatus"/>
              </template>
            </el-table-column>
            <el-table-column
              :key="Math.random()"
              label="操作"
              width="146"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="{ row }">
                <!-- v-permission="['APPLICATIONSYSTEM_UPDATEBUTTON_BUTTON']" -->
                <el-button
                  size="mini"
                  :disabled="row.buttonType==0"
                  @click="handleEdit('update', row)"
                >
                  编辑
                </el-button>
                <!-- v-permission="['APPLICATIONSYSTEM_DELBUTTON_BUTTON']" -->
                <el-button
                  slot="reference"
                  size="mini"
                  type="danger"
                  :disabled="row.buttonType==0"
                  @click="handleDelete(row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="el-drawer__footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="isLoading" @click="handleConfirm">保存</el-button>

      </div>
    </el-drawer>
    <button-manage ref="buttonManageRef" />
  </div>
</template>
<script>
import buttonManage from './buttonManage.vue'
import buttonConfigApi from '@/framework/api/developerCenter/application-system/buttonConfig'
import { deleteSysMenuButton } from '@/framework/api/menu'

export default {
  name: 'ApplicationSystem',
  components: {
    buttonManage
  },
  data() {
    return {
      drawer: false,
      direction: 'rtl',
      loading: false,
      multipleSelection: [],
      isLoading: false,
      formId: '',
      menuId: '',
      buttonList: [],
      isIndeterminate: false,
      checkAll: false,
      queryParams: {}
    }
  },
  computed: {
    size() {
      return this.$store.getters.device === 'mobile' ? '375px' : '960px'
    }
  },
  methods: {
    close() {
      this.$dtMessageBox({ title: '提示', message: '检测到您有未保存的数据，请保存数据后退出', type: 'warning', confirmButtonText: '保存', cancelButtonText: '不保存' }).then(() => {
        this.handleConfirm()
      }).catch(() => {
        this.drawer = false
      })
    },
    handleDelete(row) {
      this.$dtMessageBox({ title: '提示', message: `确定要删除“${row.label}”吗？删除后该按钮及其配置将一同删除`, type: 'warning' }).then(() => {
        deleteSysMenuButton({ buttonId: row.buttonId }).then((res) => {
          this.getList()
          this.$dtMessage({
            message: '删除成功',
            type: 'success'
          })
        }).catch(() => {
          // 取消不做操作
        })
      })
    },
    openDrawer() {
      this.drawer = true
      this.queryParams = {
        appId: this.$route.query.appId,
        formId: this.$route.query.formId,
        menuId: this.$route.query.menuId
      }
      this.getList()
    },
    getList() {
      this.loading = true
      buttonConfigApi.getLowCodeButtonList({ formId: this.queryParams.formId }).then((res) => {
        this.buttonList = res.data
        this.loading = false
        this.changeStatus()
      }).catch(() => {
        this.loading = false
      })
    },
    handleEdit(type, row) {
      this.$refs.buttonManageRef.openDialog(type, row, this.queryParams)
    },
    handleSelectionChange(val) {
      this.buttonList = this.buttonList.map((res) => {
        res.status = val ? 1 : 0
        return res
      })
      this.isIndeterminate = false
    },
    handleConfirm() {
      this.isLoading = true
      const buttonStatusList = this.buttonList.map((item) => {
        return {
          buttonId: item.buttonId,
          status: item.status
        }
      })
      buttonConfigApi.setLowCodeButtonStatus({ formId: this.queryParams.formId, buttonStatusList }).then((res) => {
        this.$dtMessage({
          message: '保存成功',
          type: 'success'
        })
        this.getList()
        this.$parent.getBtnList()
        this.isLoading = false
        this.drawer = false
      }).catch(() => {
        this.isLoading = false
      })
    },
    changeStatus(val) {
      const list = this.buttonList.filter((item) => item.status == 1)
      this.checkAll = list.length == this.buttonList.length
      this.isIndeterminate = list.length > 0 && list.length < this.buttonList.length
    }
  }
}
</script>
  <style lang="scss" scoped>
    ::v-deep .drawerWithButton .el-drawer__header{
    margin:20px 24px;
    color: #1D2129;
  }
  ::v-deep .el-drawer__body{
    box-sizing: border-box;
    overflow: auto;
    padding: 16px ;
  }
  .table-opt-container {
    display: flex;
    margin-bottom: 16px;
    justify-content: space-between;
  }
  .button{
    display: flex;
    text-align: center;
    justify-content: flex-end;
  }
  </style>

